package com.lwl.blog.dao.impl;

import com.lwl.blog.dao.InvitataionDao;
import com.lwl.blog.entity.InvitataionEntity;
import com.lwl.blog.entity.UserEntity;
import com.lwl.blog.utils.SystemUserUtil;
import com.lwl.blog.vo.IndexDataVo;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class InvitataionDaoImpl implements InvitataionDao {
    //获得连接池
    QueryRunner qr = new QueryRunner(SystemUserUtil.ds);

    @Override
    public Integer insert(InvitataionEntity in) throws SQLException {
        String sql = "insert into t_discuss_post(user_id,title,content,type,status,create_time,comment_count,score) values(?,?,?,?,?,?,?,?)";
        return qr.update(sql, in.getUserId(), in.getTitle(), in.getContent(), in.getType(), in.getStatus(), in.getCreateTime(), in.getCommentCount(), in.getScore());
    }

    @Override
    public Integer updateById(InvitataionEntity invitataionEntity) throws SQLException {
        return null;
    }

    @Override
    public Integer deleteById(Integer id) throws SQLException {
        return null;
    }

    @Override
    public List<InvitataionEntity> selectList() throws SQLException {
        return null;
    }

    @Override
    public InvitataionEntity selectById(Integer id) throws SQLException {
        return null;
    }

    @Override
    public Integer batchRemove(List<Integer> ids) throws SQLException {
        return null;
    }

    @Override
    public List<IndexDataVo> indexDateList() throws SQLException {
        String sql = "select d.id, d.user_id AS userId, d.title, d.content, d.type, d.status, d.create_time AS createTime, d.score, u.username, u.header_url FROM t_discuss_post d LEFT JOIN t_user u ON u.id = d.user_id";
        List<IndexDataVo> list = qr.query(sql, new BeanListHandler<IndexDataVo>(IndexDataVo.class) {

            @Override
            public List<IndexDataVo> handle(ResultSet r) throws SQLException {
                //1.准备一个集合春暖查询出来的系信息
                List<IndexDataVo> idvList = new ArrayList<>();

                while (r.next()) {
                    //2.创建一个帖子和用户对象
                    IndexDataVo indexDataVo = new IndexDataVo();

                    //3.将创建一个用户和帖子实体类对象
                    InvitataionEntity invitataionEntity = new InvitataionEntity();
                    UserEntity userEntity = new UserEntity();

                    //4.将获得的集合对象中的数据取出来，然后封装到帖表对象中去·
                    //id，userId，title，content，type，status，createTime，username ，header_url
                    int id=r.getInt("id");
                    int userId=r.getInt("userId");
                    String title=r.getString("title");
                    String content=r.getString("content");
                    int type=r.getInt("type");
                    int status=r.getInt("status");
                    Date createTime=r.getDate("createTime");
                    String username=r.getString("username");
                    String header_url=r.getString("header_url");

                    invitataionEntity.setId(id);
                    invitataionEntity.setUserId(userId);
                    invitataionEntity.setTitle(title);
                    invitataionEntity.setContent(content);
                    invitataionEntity.setType(type);
                    invitataionEntity.setStatus(status);
                    invitataionEntity.setCreateTime(createTime);
                    userEntity.setUsername(username);
                    userEntity.setHeaderUrl(header_url);

                    //放入到帖子对象中
                    indexDataVo.setInvitataionEntity(invitataionEntity);
                    indexDataVo.setUserEntity(userEntity);

                    //存入集合
                    idvList.add(indexDataVo);
                    System.out.println("InvitataionDaoImpl层输出："+idvList);
                }
                return idvList;
            }
        });
        return list;

    }
}
